﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SpringStudio.Common;
using Sdepci.InfoCode.BLL.Authority;
using Sdepci.InfoCode.BLL.CodeDesign;

namespace Sdepci.InfoCode.Web.CodeDesign
{
    public partial class InfoEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindData();
            }
        }

        private void BindData()
        {
            if (this.Request.QueryString["RuleGuid"] != null && this.Request.QueryString["RuleGuid"].Trim().Length > 0)
            {
                TypeRule rule = new TypeRule(this.Request.QueryString["RuleGuid"].Trim());
                this.lblTitle.Text = rule.RuleName;
                this.lblRuleDemo.Text = rule.RuleDemo;
                this.RuleGuid.Value = rule.RuleGuid;
                this.RuleModule.Value = rule.RuleModule;
            }
            else
            {
                this.Response.Clear();
                this.Response.Write("<script language=\"javascript\">alert(\"参数错误！请与系统管理员联系。\");self.close();</script>");
                this.Response.End();
            }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            this.Response.Clear();
            this.Response.Write(this.Save());
            this.Response.End();
        }

        private string Save()
        {
            string infoCode, infoDesc, ruleGuid, createUserId, serialReference, serialNumber;

            // 判断参数的有效性
            if (!"".Equals(this.InfoCode.Value))
            {
                infoCode = this.InfoCode.Value;
                infoDesc = this.InfoDesc.Text;
            }
            else
            {
                return "<script language=\"javascript\">alert(\"系统错误：编码为空，未能获取！请与系统管理员联系。\");self.close();</script>";
            }

            if (!"".Equals(this.RuleGuid.Value))
            {
                ruleGuid = this.RuleGuid.Value;
            }
            else
            {
                return "<script language=\"javascript\">alert(\"系统错误：编码类型为空！请与系统管理员联系。\");self.close();</script>";
            }

            if (Session["User"] != null)
            {
                createUserId = (Session["User"] as User).UserId;
            }
            else
            {
                return "<script language=\"javascript\">alert(\"系统错误：操作超时，请重新登录！\");self.close();</script>";
            }

            if (!"".Equals(this.SerialReference.Value))
            {
                serialReference = this.SerialReference.Value;
            }
            else
            {
                return "<script language=\"javascript\">alert(\"系统错误：流水号参考码为空，未能获取！请与系统管理员联系。\");self.close();</script>";
            }

            if (!"".Equals(this.SerialNumber.Value))
            {
                serialNumber = this.SerialNumber.Value;
            }
            else
            {
                return "<script language=\"javascript\">alert(\"系统错误：当前流水号为空，未能获取！请与系统管理员联系。\");self.close();</script>";
            }

            InfoManager info = new InfoManager();
            // 判断编码是否已存在（往往在用户保存操作超时的情况下发生）
            if (info.Exist(ruleGuid, infoCode))
            {
                return "<script language=\"javascript\">alert(\"编码保存失败，请重新创建！(可能的原因：操作超时，该编码已被其他用户占用。)\");self.close();</script>";
            }

            // 保存详细信息编码
            if (info.Add(infoCode, infoDesc, ruleGuid, createUserId, serialReference, serialNumber))
            {
                // 再保存简要信息编码
                new BriefManager().Save(infoCode, infoDesc, ruleGuid, createUserId, serialReference, serialNumber);
                return "<script language=\"javascript\">window.returnValue='" + infoCode + "';self.close();</script>";
            }
            else
            {
                return "<script language=\"javascript\">alert(\"编码保存失败！请与系统管理员联系。\");self.close();</script>";
            }
        }
    }
}
